package org.lisa.crm.controller.customer;

import org.lisa.crm.common.util.Result;
import org.lisa.crm.framework.context.BaseController;
import org.lisa.crm.model.Customer;
import org.lisa.crm.model.CustomerOrder;
import org.lisa.crm.model.DataDic;
import org.lisa.crm.model.SaleChance;
import org.lisa.crm.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/1/15.
 */
@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseController {

    @Autowired
    private CustomerService customerService;

    @PostMapping("queryAllCustomers")
    @ResponseBody
    public  List<Customer> queryAllCustomers(){
        return customerService.queryAllCustomers();
    }


    @GetMapping("/index.shtml")
    public  String index(){
        return "customer";
    }

    @RequestMapping("queryCustomersByParams")
    @ResponseBody
    public Map<String,Object> queryCustomersByParams(Customer customer){

        return customerService.queryCustomersByParams(customer);
    }


    //+"/customer/insert";
    @RequestMapping("insert")
    @ResponseBody
    public Result saveCustomer(Customer customer){

        return customerService.saveCustomer(customer);
    }

    //+"/customer/update";
    @RequestMapping("update")
    @ResponseBody
    public Result updateCustomer(Customer customer){

        return customerService.updateCustomer(customer);
    }

    ///customer/delete"
    @RequestMapping("delete")
    @ResponseBody
    public Result deleteCustomer(int ids){

        return customerService.deleteCustomer(ids);
    }

    ///customer/queryDataDicValueByDataDicName?dataDicName=客户等级"
    @RequestMapping("queryDataDicValueByDataDicName")
    @ResponseBody
    public List<DataDic> queryDataDicValueByDataDicName(String dataDicName){

        return customerService.queryDataDicValueByDataDicName(dataDicName);

    }

    // ctx+"/customer/toOtherPage/"+type+"/"+rows[0].id);
    @RequestMapping("toOtherPage/{type}/{id}")
    public ModelAndView toOtherPage(@PathVariable("type") int  type , @PathVariable("id") int  id){

        /*
        既要跳转页面又要携带数据 使用  ModelAndView
         */
        String res = "";
        switch (type){
            case 1 :
                res = "customer_linkman" ;
                break;
            case 2 :
                res = "customer_contact" ;
                break;
            case 3 :
                res = "customer_order";

        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName(res);
        Customer customer = customerService.queryCusBycId(id);
        mv.addObject("customer",customer);

        return  mv;

    }


}
